﻿package {
	import alternativa.engine3d.alternativa3d;
	import alternativa.engine3d.core.Camera3D;
	import alternativa.engine3d.display.Skin;
	import alternativa.engine3d.materials.Material;
	import alternativa.engine3d.materials.TextureMaterial;
	import alternativa.engine3d.materials.TextureMaterialPrecision;
	import alternativa.types.Texture;
	
	import flash.display.BlendMode;
	import flash.geom.ColorTransform;
	
	use namespace alternativa3d;

	public class TextureMaterialExt extends TextureMaterial {
		
		// Default color transformation
		private static const defaultColorTransfrom:ColorTransform = new ColorTransform();
		// Current color transformation
		private var _colorTransfrom:ColorTransform = new ColorTransform();
		
		/**
		 * 
		 */
		public function TextureMaterialExt(texture:Texture, alpha:Number=1, repeat:Boolean=true, smooth:Boolean=false, blendMode:String=BlendMode.NORMAL, wireThickness:Number = -1, wireColor:uint=0, precision:Number=TextureMaterialPrecision.MEDIUM) {
			super(texture, alpha, repeat, smooth, blendMode, wireThickness, wireColor, precision);
		}
		
		/**
		 * Color transformation of the material.
		 */
		public function get colorTransfrom():ColorTransform {
			return new ColorTransform(_colorTransfrom.redMultiplier, _colorTransfrom.greenMultiplier, _colorTransfrom.blueMultiplier, _colorTransfrom.alphaMultiplier, _colorTransfrom.redOffset, _colorTransfrom.greenOffset, _colorTransfrom.blueOffset, _colorTransfrom.alphaOffset);
		}
		
		/**
		 * @private
		 */
		public function set colorTransform(value:ColorTransform):void {
			if (value == null) {
				throw ArgumentError("Color transform cannot be null");
			}
			_colorTransfrom.redMultiplier = value.redMultiplier;
			_colorTransfrom.greenMultiplier = value.greenMultiplier;
			_colorTransfrom.blueMultiplier = value.blueMultiplier;
			_colorTransfrom.alphaMultiplier = value.alphaMultiplier;
			_colorTransfrom.redOffset = value.redOffset;
			_colorTransfrom.greenOffset = value.greenOffset;
			_colorTransfrom.blueOffset = value.blueOffset;
			_colorTransfrom.alphaOffset = value.alphaOffset;
			if (_surface != null) {
				_surface.addMaterialChangedOperationToScene();
			}
		}
		
		/**
		 * @inheritDoc
		 */
		override public function get alpha():Number {
			return _colorTransfrom.alphaMultiplier;
		}
		
		/**
		 * @private
		 */
		override public function set alpha(value:Number):void {
			super.alpha = value;
			_colorTransfrom.alphaMultiplier = value;
			if (_surface != null) {
				_surface.addMaterialChangedOperationToScene();
			}
		}
		
		/**
		 * @inheritDoc
		 */
		override alternativa3d function clear(skin:Skin):void {
			super.clear(skin);
			skin.transform.colorTransform = defaultColorTransfrom;
		}
		
		/**
		 * @inheritDoc
		 */
		override alternativa3d function draw(camera:Camera3D, skin:Skin, length:uint, points:Array):void {
			super.draw(camera, skin, length, points);
			skin.transform.colorTransform = _colorTransfrom;
		}
		
		/**
		 * @inheritDoc
		 */
		override public function clone():Material {
			var res:TextureMaterialExt = new TextureMaterialExt(_texture, _alpha, _repeat, _smooth, _blendMode, _wireThickness, _wireColor, _precision);
			return res;
		}
	}
}